package com.ziqikeji.xianyan.controller.examine;

import com.ziqikeji.xianyan.common.DatagridDTO;
import com.ziqikeji.xianyan.entity.article.Article;
import com.ziqikeji.xianyan.enumeration.YesOrNoEnum;
import com.ziqikeji.xianyan.service.article.ArticleService;
import org.hibernate.mapping.Array;
import org.hibernate.mapping.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author cyy
 * Created by home on 2019/9/26.
 * 审核专区控制器
 */
@Controller
@RequestMapping(value = "admin/examine")
public class ExamineController {

    @Autowired
    private ArticleService articleService;

    /**
     * 文章审核数据列表
     */
    @ResponseBody
    @RequestMapping(value = "/articleExamineList",method = RequestMethod.GET)
    public DatagridDTO getArticleExamineList(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") String pageNumber,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") String pageSize
                                             ){
        Page pageImpl = null;
        try{
            int page = 0;
            int size = 10;
            if (!StringUtils.isEmpty(pageNumber)) {
                page = Integer.parseInt(pageNumber) - 1;
                page = page < 0 ? 0 : page;
            }
            if (!StringUtils.isEmpty(pageSize)) {
                size = Integer.parseInt(pageSize);
                size = size < 0 ? 0 : size;
            }
            Page<Article> articlePage = this.articleService.getPage(page,size);
            PageRequest pageRequest = PageRequest.of(page,size);
            pageImpl = new PageImpl<>(articlePage.getContent(), pageRequest, articlePage.getTotalElements());

        }catch (Exception e){
            e.printStackTrace();
            return new DatagridDTO("获取失败，错误信息：" + e.getMessage(), 1, new ArrayList());
        }
        return new DatagridDTO("列表获取成功", 0, pageImpl.getTotalElements(), pageImpl.getContent());
    }

    /**
     * 文章已审核数据列表
     */
    @ResponseBody
    @RequestMapping(value = "/articleExamineList/Yes",method = RequestMethod.GET)
    public DatagridDTO getArticleExamineListYes(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") String pageNumber,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") String pageSize
    ){
        Page pageImpl = null;
        try{
            int page = 0;
            int size = 10;
            if (!StringUtils.isEmpty(pageNumber)) {
                page = Integer.parseInt(pageNumber) - 1;
                page = page < 0 ? 0 : page;
            }
            if (!StringUtils.isEmpty(pageSize)) {
                size = Integer.parseInt(pageSize);
                size = size < 0 ? 0 : size;
            }
            Page<Article> articlePage = this.articleService.getPageYes(page,size);
            PageRequest pageRequest = PageRequest.of(page,size);
            pageImpl = new PageImpl<>(articlePage.getContent(), pageRequest, articlePage.getTotalElements());

        }catch (Exception e){
            e.printStackTrace();
            return new DatagridDTO("获取失败，错误信息：" + e.getMessage(), 1, new ArrayList());
        }
        return new DatagridDTO("列表获取成功", 0, pageImpl.getTotalElements(), pageImpl.getContent());
    }

    /**
     * 审核文章
     */
    @RequestMapping(value = "articleExaminePass/{id}",method = RequestMethod.POST)
    public ResponseEntity articleExaminePass(@PathVariable("id")String id){
        try{
            Article article = this.articleService.get(id);
            article.setIsPassed(YesOrNoEnum.YES);
            this.articleService.update(article);
        }catch (Exception e){
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 查看
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "detail/{id}")
    public String detail(@PathVariable("id")String id,Model model){
        Article article = this.articleService.get(id);
        model.addAttribute("article",article);
        return "admin/examine/article/detail";
    }


}